package com.trkj.controller;


import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.trkj.pojo.Emp;
import com.trkj.service.EmpRoleService;
import com.trkj.service.EmpService;
import com.trkj.vo.EmpAndDeptVo;
import com.trkj.vo.Information;

/**
 * 
 * @author skm
 *
 */

@Controller
@SessionAttributes({"emp", "perids"})
public class UserController {
	@Autowired
	EmpService empService;
	@Autowired
	EmpRoleService empRoleService;
	/**
	 * @author skm
	 * @return
	 */
	@GetMapping("/to_login")
	public String to_login(){
		return "login";
	}//	
	
	/**
	 * @author skm
	 * @return
	 */
	@GetMapping("/to_index")
	public String index() {
		return "index";
	}
	
	 /**
     * 登陆
     * @param name
     * @param pass
     * @return
     * @author skm
     */
    @RequestMapping("/login")
    @ResponseBody
    public Information login(String name, String pass, Model model){
        Emp emp = empService.login(name, pass);
        if(emp == null){
            //登陆失败
            return new Information("对不起，用户名、密码不正确，或者您暂无登陆权限，请与管理员联系！");
        }else{
            //登陆成功
            model.addAttribute("emp", emp);
            //查询员工的所有功能id并且放入session中
            Set<Integer> perids = empRoleService.queryPermissionsIdByEmpId(emp.getEmpId());
            model.addAttribute("perids", perids);
            return new Information();
        }
    }
    
    /**
     * 注销
     * @param ss
     * @return
     * @author skm
     */
    @RequestMapping("/logout")
    public String logout(SessionStatus ss){
        ss.setComplete();
        return "redirect:/to_login";
    }

    /**
     * 修改密码
     * @param model
     * @param oldPassword
     * @param empPassword1
     * @param empPassword2
     * @return
     * @author skm
     */
    @PostMapping("/edit_emp_password")
    @ResponseBody
    public Information editEmpPassword(Model model,
            String oldPassword,
            String empPassword1,
            String empPassword2){
        try {
            Emp emp = (Emp) model.asMap().get("emp");
            Integer empId = emp.getEmpId();
            if(!empPassword1.equals(empPassword2))
                return Information.getInstance(false, "两次密码不一致。");
            boolean b = empService.editEmpPassword(empId, oldPassword, empPassword1);
            if(b){
                return new Information();
            }else{
                return Information.getInstance(false, "旧密码不正确。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Information.getInstance(false, e.getMessage());
        }
    }
    
    /**
     * 查询个人信息请求的界面
     * @return
     * @author skm
     */
    @RequestMapping("/LookOneEmpInformation")
    public String LookOneEmpInformation() {
    	return "LookOneEmpInformation";
    }
    
    /**
     * 查询个人详细信息
     * @param model
     * @return
     * @author skm
     */
    @RequestMapping("/getempinfo")
    @ResponseBody
    public EmpAndDeptVo getempinfo(Model model) {
    	Emp emp = (Emp) model.asMap().get("emp");
    	EmpAndDeptVo empAndDept = empService.selectEmpAndDept(emp.getEmpId());
		return empAndDept;
	}
    
    /**
     * 请求修改的地址
     * @return
     * @author skm
     */
    @RequestMapping("/UpdateOneEmpInformation")
    public String UpdateOneEmpInformation() {
    	return "UpdateOneEmpInformation";
    }
    
    /**
     * 修改个人的信息
     * @param model
     * @return
     * @author skm
     */
    @RequestMapping("/getEmpEitdInformation")
    @ResponseBody
    public boolean getEmpEitdInformation(Emp emp,Model model) {
    	Integer emp1=empService.updateEmpInformation(emp,emp.getEmpId());
    	if(emp1!=0) {
    		return true;
    	}
    	return false;
		
	}
    
}
